{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先，我们导入`mindspore`。此外，需要导入`mindspore`的`ops`API来完成部分操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mindspore as ms\n",
    "import mindspore.ops as ops"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先，我们可以使用`arange`创建一个行向量x。这个行向量包含以0开始的前12个整数，它们默认创建为整数。也可指定创建类型为浮点数。张量中的每个值都称为张量的 元素（element）。例如，张量 x 中有 12 个元素。除非额外指定，新的张量将存储在内存中，并采用基于CPU的计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[12], dtype=Int64, value= [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = ops.arange(12)\n",
    "x"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "可以通过张量的`mindspore`属性来访问张量（沿每个轴的长度）的形状 。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12,)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 4], dtype=Int64, value=\n",
       "[[ 0,  1,  2,  3],\n",
       " [ 4,  5,  6,  7],\n",
       " [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = x.reshape(3, 4)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[2, 3, 4], dtype=Float32, value=\n",
       "[[[ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n",
       "  [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n",
       "  [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00]],\n",
       " [[ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n",
       "  [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n",
       "  [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ops.zeros((2, 3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[2, 3, 4], dtype=Float32, value=\n",
       "[[[ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,  1.00000000e+00],\n",
       "  [ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,  1.00000000e+00],\n",
       "  [ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,  1.00000000e+00]],\n",
       " [[ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,  1.00000000e+00],\n",
       "  [ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,  1.00000000e+00],\n",
       "  [ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,  1.00000000e+00]]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ops.ones((2, 3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 4], dtype=Float32, value=\n",
       "[[ 1.13799882e+00, -4.56003368e-01, -8.73978078e-01,  9.31848228e-01],\n",
       " [ 1.23433046e-01,  1.21131825e+00,  1.28748190e+00,  2.70856231e-01],\n",
       " [ 7.04268515e-01, -1.69450343e+00, -9.84726071e-01, -1.32821691e+00]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ops.randn(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 4], dtype=Int64, value=\n",
       "[[2, 1, 4, 3],\n",
       " [1, 2, 3, 4],\n",
       " [4, 3, 2, 1]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ms.Tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[4], dtype=Float32, value= [ 3.00000000e+00,  4.00000000e+00,  6.00000000e+00,  1.00000000e+01]),\n",
       " Tensor(shape=[4], dtype=Float32, value= [-1.00000000e+00,  0.00000000e+00,  2.00000000e+00,  6.00000000e+00]),\n",
       " Tensor(shape=[4], dtype=Float32, value= [ 2.00000000e+00,  4.00000000e+00,  8.00000000e+00,  1.60000000e+01]),\n",
       " Tensor(shape=[4], dtype=Float32, value= [ 5.00000000e-01,  1.00000000e+00,  2.00000000e+00,  4.00000000e+00]),\n",
       " Tensor(shape=[4], dtype=Float32, value= [ 1.00000000e+00,  4.00000000e+00,  1.60000000e+01,  6.40000000e+01]))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = ms.Tensor([1.0, 2, 4, 8])\n",
    "y = ms.Tensor([2, 2, 2, 2])\n",
    "x + y, x - y, x * y, x / y, x ** y  # **运算符是求幂运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[4], dtype=Float32, value= [ 2.71828198e+00,  7.38905573e+00,  5.45981445e+01,  2.98095825e+03])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ops.exp(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[6, 4], dtype=Float32, value=\n",
       " [[ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00,  3.00000000e+00],\n",
       "  [ 4.00000000e+00,  5.00000000e+00,  6.00000000e+00,  7.00000000e+00],\n",
       "  [ 8.00000000e+00,  9.00000000e+00,  1.00000000e+01,  1.10000000e+01],\n",
       "  [ 2.00000000e+00,  1.00000000e+00,  4.00000000e+00,  3.00000000e+00],\n",
       "  [ 1.00000000e+00,  2.00000000e+00,  3.00000000e+00,  4.00000000e+00],\n",
       "  [ 4.00000000e+00,  3.00000000e+00,  2.00000000e+00,  1.00000000e+00]]),\n",
       " Tensor(shape=[3, 8], dtype=Float32, value=\n",
       " [[ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00 ...  1.00000000e+00,  4.00000000e+00,  3.00000000e+00],\n",
       "  [ 4.00000000e+00,  5.00000000e+00,  6.00000000e+00 ...  2.00000000e+00,  3.00000000e+00,  4.00000000e+00],\n",
       "  [ 8.00000000e+00,  9.00000000e+00,  1.00000000e+01 ...  3.00000000e+00,  2.00000000e+00,  1.00000000e+00]]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = ops.arange(12, dtype=ms.float32).reshape((3,4))\n",
    "Y = ms.Tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])\n",
    "ops.cat((X, Y), axis=0), ops.cat((X, Y), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 4], dtype=Bool, value=\n",
       "[[False,  True, False,  True],\n",
       " [False, False, False, False],\n",
       " [False, False, False, False]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X == Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 66)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[3, 1], dtype=Int64, value=\n",
       " [[0],\n",
       "  [1],\n",
       "  [2]]),\n",
       " Tensor(shape=[1, 2], dtype=Int64, value=\n",
       " [[0, 1]]))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = ops.arange(3).reshape((3, 1))\n",
    "b = ops.arange(2).reshape((1, 2))\n",
    "a, b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 2], dtype=Int64, value=\n",
       "[[0, 1],\n",
       " [1, 2],\n",
       " [2, 3]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[4], dtype=Float32, value= [ 8.00000000e+00,  9.00000000e+00,  1.00000000e+01,  1.10000000e+01]),\n",
       " Tensor(shape=[2, 4], dtype=Float32, value=\n",
       " [[ 4.00000000e+00,  5.00000000e+00,  6.00000000e+00,  7.00000000e+00],\n",
       "  [ 8.00000000e+00,  9.00000000e+00,  1.00000000e+01,  1.10000000e+01]]))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[-1], X[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 4], dtype=Float32, value=\n",
       "[[ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00,  3.00000000e+00],\n",
       " [ 4.00000000e+00,  5.00000000e+00,  9.00000000e+00,  7.00000000e+00],\n",
       " [ 8.00000000e+00,  9.00000000e+00,  1.00000000e+01,  1.10000000e+01]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[1, 2] = 9\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 4], dtype=Float32, value=\n",
       "[[ 1.20000000e+01,  1.20000000e+01,  1.20000000e+01,  1.20000000e+01],\n",
       " [ 1.20000000e+01,  1.20000000e+01,  1.20000000e+01,  1.20000000e+01],\n",
       " [ 8.00000000e+00,  9.00000000e+00,  1.00000000e+01,  1.10000000e+01]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[0:2, :] = 12\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "before = id(Y)\n",
    "Y = Y + X\n",
    "id(Y) == before"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "幸运的是，执行原地操作非常简单。 我们可以使用切片表示法将操作的结果分配给先前分配的数组，例如Y[:] = <expression>。 为了说明这一点，我们首先创建一个新的矩阵Z，其形状与另一个Y相同， 使用zeros_like来分配一个全的块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id(Z): 140072895924000\n",
      "id(Z): 140072895924000\n"
     ]
    }
   ],
   "source": [
    "Z = ops.zeros_like(Y)\n",
    "print('id(Z):', id(Z))\n",
    "Z[:] = X + Y\n",
    "print('id(Z):', id(Z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果在后续计算中没有重复使用X， 我们可以使用X[:] = X + Y来减少操作的内存开销。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "before = id(X)\n",
    "X[:] = X + Y\n",
    "id(X) == before"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将深度学习框架定义的张量转换为NumPy张量（ndarray）很容易，反之也同样容易。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(numpy.ndarray, mindspore.common.tensor.Tensor)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = X.numpy()\n",
    "B = ms.Tensor(A)\n",
    "type(A), type(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[1], dtype=Float32, value= [ 3.50000000e+00]),\n",
       " Tensor(shape=[], dtype=Float32, value= 3.5),\n",
       " 3.5,\n",
       " 3)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = ms.Tensor([3.5])\n",
    "# mindspore里item()返回的是tensor标量，而不是python标量\n",
    "a, a.item(), float(a), int(a)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mindspore2.0",
   "language": "python",
   "name": "mindspore2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
